<template>
  <div class="out-div">
    <div class="login-box">
      <!-- 表单的名字，改成 loginForm -->
      <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="用户账号：" prop="userName">
          <el-input type="text" v-model="loginForm.userName" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="用户密码：" prop="password">
          <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>

          <el-col :span="10">
            <el-button type="primary" @click="submitForm('loginForm')">登录系统</el-button>
          </el-col>

          <el-col :span="10">
            <el-button @click="resetForm('loginForm')">重置数据</el-button>
          </el-col>

        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

// import {Message} from "element-ui";

export default {
  name: "LoginView",
  data() {
    return {
      loginForm: {
        userName: '',
        password: ''
      },
      rules: {
        userName: [
          {required: true, message: '用户名不能为空', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '密码不能为空', trigger: 'change'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 创建一个参数对象，里面存了用户名和密码。
          let params = new URLSearchParams();
          params.append("userName", this.loginForm.userName);
          params.append("password", this.loginForm.password);
          // 这里应该是请求后台的数据。再确定是否登录成功
          this.$axios.post("/api/login", params)
              .then(res => {
                console.log(res)
                console.log("-----update------")
                if (res.data.status == 200) {
                  console.log("----login ok ----")
                  // 登录成功之后，我们得到从后端传过来的token
                  let token = res.headers['token'];
                  let token2 = res.headers.get('token');

                  console.log("得到的token: " + token)
                  console.log("得到的token2: " + token2)
                  // 把token存在sessionStore里面
                  sessionStorage.setItem("token", token);

                  // this.$router.push({path: '/main'});
                  this.$router.push({path: '/welcome'});
                }
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<!-- 局部引入css : 这样是没有body的 -->
<style scoped src="@/css/login-view.css"></style>
<style scoped>
/*  这里写其它的样式，只在本页使用。  */
</style>


